package com.tanhua.controller;

import com.tanhua.domain.mongo.Publish;
import com.tanhua.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import java.util.Map;

@RestController
@RequestMapping("/manage")
public class UsersController {
    @Autowired
    private UsersService usersService;

    /**
     * 用户管理-用户列表
     * GET /manage/users
     */
    @GetMapping("/users")
    public ResponseEntity getUsers(@RequestParam(defaultValue = "1") int page,
                                  @RequestParam(defaultValue = "10") int pagesize) {
        return usersService.getUsers(page, pagesize);
    }

    /**
     * 用户管理-用户基本资料
     * GET /manage/users/:userID
     */
    @GetMapping("/users/{userID}")
    public ResponseEntity getUser(@PathVariable("userID") Long userID) {
        return usersService.getUser(userID);
    }

    /**
     * 用户管理-动态分页
     * GET /manage/messages
     *//*
    @GetMapping("/messages")
    public ResponseEntity findMessages(@RequestParam(defaultValue = "1") int page,
                                       @RequestParam(defaultValue = "10") int pagesize,
                                       @RequestParam(required = false) Long uid,
                                       @RequestParam(required = false) String state) {
        return usersService.findMessages(page, pagesize, uid, state);
    }*/

    /**
     * 用户管理-动态详情
     * GET /manage/messages/:id
     */
    @GetMapping("/messages/{id}")
    public ResponseEntity findPublishById(@PathVariable("id") String publishId) {
        return usersService.findPublishById(publishId);
    }

    /**
     * 用户管理-评论列表
     * GET /manage/messages/comments
     */
    @GetMapping("/messages/comments")
    public ResponseEntity findCommentList(@RequestParam(defaultValue = "1") int page,
                                          @RequestParam(defaultValue = "10") int pagesize,
                                          String messageID) {
        return usersService.findCommentList(page, pagesize,messageID);
    }

    /**
     * 用户管理-视频记录
     * GET /manage/videos
     */
    @GetMapping("/videos")
    public ResponseEntity videos(@RequestParam(defaultValue = "1") int page,
                                 @RequestParam(defaultValue = "10") int pagesize,
                                 @RequestParam(required = false)Long uid) {
       return usersService.findVideos(page, pagesize, uid);
    }

    /**
     * 动态审核
     * @return
     */
    @GetMapping("/messages")
    public ResponseEntity findMovementsList(@RequestParam(defaultValue = "1") int page,
                                            @RequestParam(defaultValue = "10")int pagesize,
                                            @RequestParam(required = false) Long uid,
                                            String state){
        Integer num;
        try {
            num = Integer.valueOf(state);
        } catch (Exception e) {
            num = null;
        }

        return usersService.findMovementsList(page, pagesize,uid, num);
    }

    /**
     * 用户冻结操作
     * POST/manage/users/freeze
     */
    @PostMapping("/users/freeze")
    public ResponseEntity freeze(@RequestBody Map<String,Object> map) {
        Integer userId = (Integer) map.get("userId");
        Integer freezingTime =Integer.parseInt(map.get("freezingTime").toString());
        Integer freezingRange = Integer.parseInt(map.get("freezingRange").toString());
        String reasonsForFreezing = (String) map.get("reasonsForFreezing");
        String frozenRemarks = (String) map.get("frozenRemarks");
        return usersService.freeze(userId, freezingTime, freezingRange, reasonsForFreezing, frozenRemarks);
    }

    /**
     * 用户解冻操作
     * POST/manage/users/unfreeze
     */
    @PostMapping("/users/unfreeze")
    public ResponseEntity unFreeze(@RequestBody Map<String,Object> map){
        Integer userId = (int)map.get("userId");
        String reasonsForThawing = map.get("reasonsForThawing").toString();
        return usersService.unFreeze(userId,reasonsForThawing);
    }

    /**
     * 动态通过
     * POST/manage/messages/pass
     */
    @PostMapping("/messages/pass")
    public ResponseEntity pass(@RequestBody String[] ids){
        return usersService.pass(ids);
    }
    /**
     * 动态拒绝
     * POST/manage/messages/reject
     */
    @PostMapping("/messages/reject")
    public ResponseEntity reject(@RequestBody String[] ids){
        return usersService.reject(ids);
    }
}
